package com.project.controller.web;

import com.project.common.utils.ExcelUtil;
import com.project.common.utils.MyPageHelper;
import com.project.entity.*;
import com.project.myBeans.ExerciseAnswer;
import com.project.myBeans.ExerciseDetail;
import com.project.myBeans.R;
import com.project.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@EnableAutoConfiguration
@RestController
@RequestMapping("/exercise")
@Api(description = "题目接口", value = "exercise")
public class ExerciseController {

    @Autowired
    private ExerciseInfoService exerciseInfoService;

    @Autowired
    private ChoiceService choiceService;

    @Autowired
    private TrueFalseService trueFalseService;

    @Autowired
    private CompletionService completionService;

    @Autowired
    private CompAnswerService compAnswerService;

    @Autowired
    private ShortAnswerService shortAnswerService;

    @Autowired
    private ProgrammingService programmingService;

    @Autowired
    private AnswerRecordService answerRecordService;

    @Autowired
    private ExerciseRecordService exerciseRecordService;

    @Autowired
    private ExerciseService exerciseService;

    /**
     * wj 2018/04/12
     *
     * @param exerciseDetail
     * @return
     */
    @ApiOperation("单个添加题目接口")
    @PostMapping("/addOne")
    public R addOne(@RequestBody ExerciseDetail exerciseDetail) {
        try{
            exerciseService.add(exerciseDetail);
            return R.ok().put("exerciseInfo",exerciseDetail.getExerciseInfo());
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * wwh 2018/04/11
     *
     * @param exerciseInfo
     * @return
     */
    @ApiOperation("通过条件查找题目（课程，题型，难度，知识点）")
    @PostMapping("/findByCondition")
    public R findByCondition(@RequestBody ExerciseInfo exerciseInfo) {
        try {
            List<ExerciseInfo> exerciseInfoList = exerciseInfoService.findByCondition(exerciseInfo);
            List<ExerciseDetail> exerciseDetailList = new ArrayList<>();
            for (ExerciseInfo exerciseInfo1 : exerciseInfoList) {
                ExerciseDetail exerciseDetail = new ExerciseDetail();
                exerciseDetail.setExerciseInfo(exerciseInfo1);
                switch (exerciseInfo1.getType()) {
                    case 1:
                    case 2:
                        exerciseDetail.setChoice(choiceService.findByExerciseInfoId(exerciseInfo1.getId()));
                        break;
                    case 3:
                        exerciseDetail.setTrueFalse(trueFalseService.findByExerciseInfoId(exerciseInfo1.getId()));
                        break;
                    case 4:
                        Completion completion = completionService.findByExerciseInfoId(exerciseInfo1.getId());
                        completion.setAnswerArray(compAnswerService.findByCompidOrderByIndex(completion.getId()));
                        exerciseDetail.setCompletion(completion);
                        break;
                    case 5:
                        exerciseDetail.setShortAnswer(shortAnswerService.findByExcerciseInfoId(exerciseInfo1.getId()));
                        break;
                    case 6:
                        exerciseDetail.setProgramming(programmingService.findByExerciseInfoId(exerciseInfo1.getId()));
                        break;
                }
                exerciseDetailList.add(exerciseDetail);
            }
            return R.ok().put("data", exerciseDetailList).put("total", MyPageHelper.getTotal(exerciseInfoList));
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }


    /**
     * fyk 2018/04/16
     *
     * @param exerciseDetail
     * @return
     */
    @ApiOperation("编辑题目")
    @PostMapping("/modify")
    public R modify(@RequestBody ExerciseDetail exerciseDetail) {
        try {
            ExerciseInfo exerciseInfo = exerciseDetail.getExerciseInfo();
            exerciseInfoService.modify(exerciseInfo);
            switch (exerciseInfo.getType()) {
                case 1:
                case 2:
                    choiceService.modify(exerciseDetail.getChoice());
                    break;
                case 3:
                    trueFalseService.modify(exerciseDetail.getTrueFalse());
                    break;
                case 4:
                    Completion completion = exerciseDetail.getCompletion();
                    completionService.modify(completion);
                    String[] compAnswerList = completion.getAnswerArray();
                    for (int i = 0; i < compAnswerList.length; i++) {
                        CompAnswer compAnswer = new CompAnswer();
                        compAnswer.setAnswer(compAnswerList[i]);
                        compAnswer.setCompid(completion.getId());
                        compAnswer.setIndex(i + 1);
                        compAnswerService.modify(compAnswer);
                    }
                    break;
                case 5:
                    shortAnswerService.modify(exerciseDetail.getShortAnswer());
                    break;
                case 6:
                    programmingService.modify(exerciseDetail.getProgramming());
                    break;
            }
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }

    }


    /**
     * Tate 2018/04/27
     *
     * @param exerciseAnswer
     * @return
     */
    @ApiOperation("记录练习答案")
    @PostMapping("/addAnswer")
    public R addAnswer(@RequestBody ExerciseAnswer exerciseAnswer) {
        try {
            //第一次保存
            if (answerRecordService.findbythree(exerciseAnswer.getAnswerRecord().getStudentid(), exerciseAnswer.getAnswerRecord().getExamid(), exerciseAnswer.getAnswerRecord().getExerciseinfoid()).isEmpty()) {
//                AnswerRecord ttp=new AnswerRecord();
//                ttp.setExamid(exerciseAnswer.getAnswerRecord().getExamid());
//                ttp.setExerciseinfoid(exerciseAnswer.getAnswerRecord().getExerciseinfoid());
//                ttp.setStudentid(exerciseAnswer.getAnswerRecord().getStudentid());
                answerRecordService.add(exerciseAnswer.getAnswerRecord());
                ExerciseRecord exerciseRecord = new ExerciseRecord();
                exerciseRecord.setAnswer(exerciseAnswer.getAnswer());
                AnswerRecord tmp = answerRecordService.findbythree(exerciseAnswer.getAnswerRecord().getStudentid(), exerciseAnswer.getAnswerRecord().getExamid(), exerciseAnswer.getAnswerRecord().getExerciseinfoid()).get(0);
                exerciseRecord.setAnswerrecordid(tmp.getId());
                exerciseRecordService.add(exerciseRecord);
                return R.ok("保存成功！");
            }

            //修改保存
            else {
                ExerciseRecord exerciseRecord = new ExerciseRecord();
                exerciseRecord.setAnswer(exerciseAnswer.getAnswer());
                AnswerRecord tmp = answerRecordService.findbythree(exerciseAnswer.getAnswerRecord().getStudentid(), exerciseAnswer.getAnswerRecord().getExamid(), exerciseAnswer.getAnswerRecord().getExerciseinfoid()).get(0);
                exerciseRecord.setAnswerrecordid(tmp.getId());
                exerciseRecordService.modify(exerciseRecord);
                return R.ok("修改成功!");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }

    }

    /**
     * Tate 2018/05/2
     *
     * @param answerRecord
     * @return
     */
    @ApiOperation("返回学生做题记录")
    @PostMapping("/findbyexerciseinfoidandstudentid")
    public R findbyexerciseinfoidandstudentid(@RequestBody AnswerRecord answerRecord) {
        try {
            MyPageHelper.setPageParam(answerRecord);
            if(answerRecordService.findbythree(answerRecord.getStudentid(), answerRecord.getExamid(), answerRecord.getExerciseinfoid()).isEmpty()){
                return R.ok().put("data",null);
            }
            else {
                AnswerRecord tmp = answerRecordService.findbythree(answerRecord.getStudentid(), answerRecord.getExamid(), answerRecord.getExerciseinfoid()).get(0);
                ExerciseRecord data;
                data = exerciseRecordService.findbyAnswerrecordid(tmp.getId());
                return R.ok().put("data", data);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * Tate 2018/5/16
     * @param file
     * @param request
     * @return
     */
    @ApiOperation("Excel批量添加题目")
    @PostMapping(value = "/addBatch ")
    public R addBatch(@RequestParam Integer courseid,@RequestParam(value = "file",required = false) MultipartFile file, HttpServletRequest request) throws IOException {
        try{
            //暂不支持程序题的添加.
            List<ExerciseDetail> exerciseDetails=ExcelUtil.parseExercise(file,courseid);
            for(ExerciseDetail exerciseDetail:exerciseDetails){
               exerciseService.add(exerciseDetail);
            }
            return R.ok("导入成功!");
        }catch(Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }
}